package com.serena.da.rest.data_providers.xml;

import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Spitkovskyi Igor on 8/20/2014.
 */
public class ComponentsXmlDataProvider extends XmlDataProvider {

    @DataProvider
    public static Iterator<Object[]> createFSBasicComponentDataProvider (ITestContext context) throws Exception {
        /*  EXPECTED XML STRUCTURE
            <CreateComponent>
			    <name>COMPONENT_NAME</name>
			    <description>COMPONENT_DESCRIPTION</description>
			    <base_path>BASE_PATH</base_path>
		    </CreateComponent>
        */
        //Get the input file path from the ITestContext
        String inputFile = context.getCurrentXmlTest().getParameter("filenamePath");
        int NumberOfParameters = 3;

        //We will be returning an iterator of Object arrays so create that first.
        List<Object[]> dataToBeReturned = new ArrayList<Object[]>();
        //Will use an array as temporary container of parameters of component creation.
        String [] parameters = new String [NumberOfParameters];

        //Pass name of file
        NodeList CreateComponentDatasets = getDatasetsFromXml(inputFile, "", "CreateComponent", NumberOfParameters);

        for(int i=0; i<CreateComponentDatasets.getLength(); i++) {
            //Get child-parameters of each "CreateComponent" block
            NodeList ComponentParametersList = CreateComponentDatasets.item(i).getChildNodes();

            //Extract values of "CreateComponent" parameters. Put values to Object[][] format, required to be returned by data provider
            for (int j = 0; j < ComponentParametersList.getLength(); j++) {
                Node node = ComponentParametersList.item(j);
                if (node.getLocalName().toLowerCase() == "name") {
                    parameters[0] = node.getTextContent();
                } else if (node.getLocalName().toLowerCase() == "description") {
                    parameters[1] = node.getTextContent();
                } else if (node.getLocalName().toLowerCase() == "base_path") {
                    parameters[2] = node.getTextContent();
                }
            }
            dataToBeReturned.add(new Object[]{parameters[0], parameters[1], parameters[2]});
        }

        //return the iterator - testng will initialize the test class and calls the
        //test method with each of the content of this iterator.
        return dataToBeReturned.iterator();
    }

    @DataProvider
    public static Iterator<Object[]> createComponentProcessDataProvider (ITestContext context) throws Exception {
        /*  EXPECTED XML STRUCTURE
            <CreateComponentProcess>
                <process_name>isAutoCompFS_3_ii_process1</name>
                <process_description>eeeeeeeeeeeeee</description>
                <process_type>Configuration Deployment</process_type>
                <component_name>isAutoCompFS_ii</component_name>
                <component_role>Admin</component_role>
            </CreateComponentProcess>
        */
        //Get the input file path from the ITestContext
        String inputFile = context.getCurrentXmlTest().getParameter("filenamePath");
        int NumberOfParameters = 5;

        //We will be returning an iterator of Object arrays so create that first.
        List<Object[]> dataToBeReturned = new ArrayList<Object[]>();
        //Will use an array as temporary container of parameters of component creation.
        String [] parameters = new String [NumberOfParameters];

        //Pass name of file
        NodeList CreateComponentDatasets = getDatasetsFromXml(inputFile, "", "CreateComponentProcess", NumberOfParameters);

        for(int i=0; i<CreateComponentDatasets.getLength(); i++) {
            //Get child-parameters of each "CreateComponent" block
            NodeList ComponentParametersList = CreateComponentDatasets.item(i).getChildNodes();

            //Extract values of "CreateComponent" parameters. Put values to Object[][] format, required to be returned by data provider
            for (int j = 0; j < ComponentParametersList.getLength(); j++) {
                Node node = ComponentParametersList.item(j);
                if (node.getLocalName().toLowerCase() == "component_name") {
                    parameters[0] = node.getTextContent();
                } else if (node.getLocalName().toLowerCase() == "process_name") {
                    parameters[1] = node.getTextContent();
                } else if (node.getLocalName().toLowerCase() == "process_description") {
                    parameters[2] = node.getTextContent();
                } else if (node.getLocalName().toLowerCase() == "process_type") {
                    parameters[3] = node.getTextContent();
                } else if (node.getLocalName().toLowerCase() == "component_role") {
                    parameters[4] = node.getTextContent();
                }
            }
            dataToBeReturned.add(new Object[]{parameters[0], parameters[1], parameters[2], parameters[3], parameters[4]});
        }

        //return the iterator - testng will initialize the test class and calls the
        //test method with each of the content of this iterator.
        return dataToBeReturned.iterator();
    }

    @DataProvider
    public static Iterator<Object[]> createComponentProcessWorkflowDataProvider (ITestContext context) throws Exception {
        /*  EXPECTED XML STRUCTURE
				<CreateComponentProcessWorkflow>
					<component_name>isAutoCompFS_3_nn</component_name>
					<process_name>isAutoCompFS_3_nn_process2</process_name>
					<!-- Supported multiple values of step_names, plugin_names, command_names elements. NOTE: respective values should match. -->
					<step_names>upload_hhirtifacts1;download_hhirtifacts2</step_names>
					<plugin_names>Serena RA Versioned File Storage;Serena RA Versioned File Storage</plugin_names>
					<command_names>Upload Artifacts;Download Artifacts</command_names>
				</CreateComponentProcessWorkflow>
        */
        //Get the input file path from the ITestContext
        String inputFile = context.getCurrentXmlTest().getParameter("filenamePath");
        int NumberOfParameters = 5;

        //We will be returning an iterator of Object arrays so create that first.
        List<Object[]> dataToBeReturned = new ArrayList<Object[]>();
        //Will use an array as temporary container of parameters of component creation.
        String [] parameters = new String [NumberOfParameters];

        //Pass name of file
        NodeList CreateComponentDatasets = getDatasetsFromXml(inputFile, "", "CreateComponentProcessWorkflow", NumberOfParameters);

        for(int i=0; i<CreateComponentDatasets.getLength(); i++) {
            //Get child-parameters of each "CreateComponent" block
            NodeList ComponentParametersList = CreateComponentDatasets.item(i).getChildNodes();

            //Extract values of "CreateComponent" parameters. Put values to Object[][] format, required to be returned by data provider
            for (int j = 0; j < ComponentParametersList.getLength(); j++) {
                Node node = ComponentParametersList.item(j);
                if (node.getLocalName().toLowerCase() == "component_name") {
                    parameters[0] = node.getTextContent();
                } else if (node.getLocalName().toLowerCase() == "process_name") {
                    parameters[1] = node.getTextContent();
                } else if (node.getLocalName().toLowerCase() == "step_names") {
                    parameters[2] = node.getTextContent();
                } else if (node.getLocalName().toLowerCase() == "plugin_names") {
                    parameters[3] = node.getTextContent();
                } else if (node.getLocalName().toLowerCase() == "command_names") {
                    parameters[4] = node.getTextContent();
                }
            }
            dataToBeReturned.add(new Object[]{parameters[0], parameters[1], parameters[2], parameters[3], parameters[4]});
        }

        //return the iterator - testng will initialize the test class and calls the
        //test method with each of the content of this iterator.
        return dataToBeReturned.iterator();
    }
}
